//
//  YYETabBarItem.swift
//  weibo14
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YYETabBarItem: UITabBarItem {
    
    override var badgeValue: String?{
    
        didSet{
            
            // 通过查看 Xcode 6.4 可以发现一个 _target ,获取打印其类型,就是我们想要的
            let target = self.valueForKey("_target") as! UITabBarController
            
            for tabBarSubView in target.tabBar.subviews {
                // 判断是否是 UITabBarButton
                if tabBarSubView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for tabBarButtonSubView in tabBarSubView.subviews {
                        // 判断是否是 _UIBadgeView
                        if tabBarButtonSubView.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            for badgeSubView in tabBarButtonSubView.subviews {
                                if badgeSubView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    print("终于找到你,还好没放弃")
                                    badgeSubView.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
